/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#ifndef SX_COLLISION_MODEL_H
#define SX_COLLISION_MODEL_H

#include <sxCollisionDetectionHandler.h>
#include <sxCollisionResponseHandler.h>

namespace Sx {
namespace Physics {
namespace Collision {

template <class ObjectType>
class AbstractCollisionModel {
public:
	virtual bool construct(Util::ArrayList<std::shared_ptr<ObjectType> >& objects) = 0;
	virtual bool update(Util::ArrayList<std::shared_ptr<ObjectType> >& objects) = 0;
	virtual bool resolveCollisions(Util::ArrayList<std::shared_ptr<ObjectType> >& objects, double dt) = 0;
	virtual bool reset(Util::ArrayList<std::shared_ptr<ObjectType> >& objects) = 0;

	virtual bool initializeCollisionEventAggregate(Util::ArrayList<std::shared_ptr<ObjectType> >& objects) = 0;
};

/*
 * A collision model represents the state of an environment that contains
 * one or more objects at a specific timestep.
 */
template <class ObjectType, class CollisionEventAggregate>
class CollisionModel : public AbstractCollisionModel<ObjectType> {
public:
	CollisionModel();
	CollisionModel(const CollisionModel& model);
	virtual ~CollisionModel();

	bool construct(Util::ArrayList<std::shared_ptr<ObjectType> >& objects);
	bool update(Util::ArrayList<std::shared_ptr<ObjectType> >& objects);
	bool resolveCollisions(Util::ArrayList<std::shared_ptr<ObjectType> >& objects, double dt);

	virtual bool initializeCollisionEventAggregate(Util::ArrayList<std::shared_ptr<ObjectType> >& objects) = 0;

	void setCollisionDetectionHanlder(Handlers::CollisionDetectionHandler<ObjectType, CollisionEventAggregate>* handler);
	void setCollisionResponseHandler(Handlers::CollisionResponseHandler<ObjectType, CollisionEventAggregate>* handler);

protected:
	CollisionEventAggregate* collisionData;
	Handlers::CollisionDetectionHandler<ObjectType, CollisionEventAggregate>* detectionHandler;
	Handlers::CollisionResponseHandler<ObjectType, CollisionEventAggregate>* responseHandler;
};

template <typename ObjectType, class CollisionEventAggregate>
CollisionModel<ObjectType, CollisionEventAggregate>::CollisionModel() {
	this->collisionData = nullptr;
	this->detectionHandler = nullptr;
	this->responseHandler = nullptr;
}

template <typename ObjectType, class CollisionEventAggregate>
CollisionModel<ObjectType, CollisionEventAggregate>::CollisionModel(const CollisionModel& model) {
	//--------------------------------------------------------------------------
	// Don't allocate the collisionData here. It is done by the class that
	// overrides the initializeCollisionEventAggregate(..) function.
	//--------------------------------------------------------------------------
	//this->detectionHandler = new Handlers::CollisionDetectionHandler<ObjectType, CollisionEventAggregate>(*model.detectionHandler);
	//this->responseHandler = new Handlers::CollisionResponseHandler<ObjectType, CollisionEventAggregate>(*model.responseHandler);
}

template <typename ObjectType, class CollisionEventAggregate>
CollisionModel<ObjectType, CollisionEventAggregate>::~CollisionModel() {
	delete this->collisionData;
	delete this->detectionHandler;
	delete this->responseHandler;
}

template <typename ObjectType, class CollisionEventAggregate>
bool CollisionModel<ObjectType, CollisionEventAggregate>::construct(Util::ArrayList<std::shared_ptr<ObjectType> >& objects) {
	if ( this->detectionHandler == nullptr ) {
		std::cerr << "[CollisionModel:construct] Error: The collision detection handler has not been set." << std::endl;
		std::cerr << "  Aborting collision model construction." << std::endl;
		return false;
	}

	if ( this->responseHandler == nullptr ) {
		std::cerr << "[CollisionModel:construct] Error: The collision response handler has not been set." << std::endl;
		std::cerr << "  Aborting collision model construction." << std::endl;
		return false;
	}

	this->detectionHandler->construct(objects);
	this->responseHandler->construct(objects);
	this->initializeCollisionEventAggregate(objects);

	return true;
}

template <typename ObjectType, class CollisionEventAggregate>
bool CollisionModel<ObjectType, CollisionEventAggregate>::update(Util::ArrayList<std::shared_ptr<ObjectType> >& objects) {
	if ( this->detectionHandler == nullptr ) return false;
	if ( this->responseHandler == nullptr ) return false;

	this->detectionHandler->update(objects, this->collisionData);
	this->responseHandler->update(objects, this->collisionData);
	return true;
}

template <typename ObjectType, class CollisionEventAggregate>
bool CollisionModel<ObjectType, CollisionEventAggregate>::resolveCollisions(Util::ArrayList<std::shared_ptr<ObjectType> >& objects, double dt) {
	if ( this->detectionHandler == nullptr ) return false;
	if ( this->responseHandler == nullptr ) return false;

	//--------------------------------------------------------------------------
	// Clear all previous collision events
	//--------------------------------------------------------------------------
	this->collisionData->clear();

	//--------------------------------------------------------------------------
	// Simplest form of collision resolution: detect the collisions and then
	// try to provide a proper response.
	//--------------------------------------------------------------------------
	this->detectionHandler->performDetection(objects, this->collisionData);
	this->responseHandler->performResponse(objects, this->collisionData, true);
	return true;
}

template <typename ObjectType, class CollisionEventAggregate>
void CollisionModel<ObjectType, CollisionEventAggregate>::setCollisionDetectionHanlder(Handlers::CollisionDetectionHandler<ObjectType, CollisionEventAggregate>* handler) {
	this->detectionHandler = handler;
}

template <typename ObjectType, class CollisionEventAggregate>
void CollisionModel<ObjectType, CollisionEventAggregate>::setCollisionResponseHandler(Handlers::CollisionResponseHandler<ObjectType, CollisionEventAggregate>* handler) {
	this->responseHandler = handler;
}

}

}

}

#endif
